package service

import (
	"errors"
	"im/database"
	"im/model"
	"time"
)

type ContactService struct {

}

func (c *ContactService) AddFriend(userId,Dstid int64) error {
	tmp := model.Contact{}
	//如果自己加自己
	if userId==Dstid {
		return errors.New("不能添加自己为好友")
	}
	//判断是否已经是好友
	database.DbCone.Where("ownerid = ?", userId).
		And("dstid = ?", Dstid).
		And("cate = ?",model.CONCAT_CATE_USER).
		Get(&tmp)
	if 0<tmp.Id {
		return errors.New("该用户已经是您的好友")
	}
	//启动事务
	session := database.DbCone.NewSession();
	session.Begin()
	//插入自己的好友记录
	_,err1 := session.InsertOne(model.Contact{
		Ownerid:userId,
		Dstobj:Dstid,
		Cate:model.CONCAT_CATE_USER,
		Createat:time.Now(),
	})
	//插入对方的好友记录
	_,err2 := session.InsertOne(model.Contact{
		Ownerid:Dstid,
		Dstobj:userId,
		Cate:model.CONCAT_CATE_USER,
		Createat:time.Now(),
	})

	if nil == err1 && nil == err2 {
		session.Commit()
	}
	if err1 != nil {
		session.Rollback()
		return err1
	}else if  err2 != nil {
		session.Rollback()
		return err2
	}
	return nil
}

//群列表
func (c *ContactService) GetLoadFriend(userId int64) ([]model.Community) {

	conconts := make([]model.Contact,0)
	comIds :=make([]int64,0)
	database.DbCone.Where("Ownerid = ? and cate = ? ", userId,model.CONCAT_CATE_USER).Find(&conconts)
	for _,v := range conconts{
		comIds = append(comIds,v.Dstobj);
	}
	coms := make([]model.Community,0)
	if len(comIds)== 0{
		return coms
	}
	database.DbCone.In("id",comIds).Find(&coms)
	return coms
}

//查找好友
func (c *ContactService) SearchFriend(userId int64) ([]model.User){
	conconts := make([]model.Contact,0)
	objIds :=make([]int64,0)
	database.DbCone.Where("ownerid = ? and cate = ?",userId,model.CONCAT_CATE_USER).Find(&conconts)
	for _,v := range conconts{
		objIds = append(objIds,v.Dstobj);
	}
	coms := make([]model.User,0)
	if len(objIds)== 0{
		return coms
	}
	database.DbCone.In("id",objIds).Find(&coms)
	return coms
}

//建群
func (c *ContactService) CreateCommunity(comm model.Community) (ret model.Community, err error)  {

	//条件判断
	if len(comm.Name) == 0 {
		return ret, errors.New("请输入群名称")
	}
	//登录
	if comm.Ownerid == 0 {
		return ret,errors.New("请先登录")
	}
	com := model.Community{
		Ownerid:comm.Ownerid,
	}
	num,err := database.DbCone.Count(&com)

	if 5 >= num {
		err = errors.New("最多可创建5个群组")
		return com,err
	} else {
		comm.Createat = time.Now()
		session := database.DbCone.NewSession()
		session.Begin()
		_,err = session.InsertOne(&comm)
		if nil != err {
			session.Rollback()
			return com,err
		}
		_,err =session.InsertOne(
			model.Contact{
				Ownerid:comm.Ownerid,
				Dstobj:comm.Id,
				Cate:model.CONCAT_CATE_COMUNITY,
				Createat:time.Now(),
			})
		if err!=nil{
			session.Rollback();
		}else{
			session.Commit()
		}
		return com,err
	}
}


//加群
func (service *ContactService) JoinCommunity(userId,comId int64) error{
	cot := model.Contact{
		Ownerid:userId,
		Dstobj:comId,
		Cate:model.CONCAT_CATE_COMUNITY,
	}
	database.DbCone.Get(&cot)
	if(cot.Id==0){
		cot.Createat = time.Now()
		_,err := database.DbCone.InsertOne(cot)
		return err
	}else{
		return nil
	}


}
